﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThuVienCT;
using System.Data.SqlClient;

namespace QLCTNA
{
    public partial class Fr_Main : Form
    {
        private bool checkClick = false;

        private ConnectionData cnn = null;
        public Fr_Main()
        {
            InitializeComponent();
            FileText ft = new FileText("Config\\Config.ini");

            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            cnn = Connection.Connect();
        }

        private void Fr_Main_Load(object sender, EventArgs e)
        {
           
            fr_ĐăngNhập frm = new fr_ĐăngNhập();
            frm.ShowDialog();
         


        }

        private void tạoMớiCôngThứcNấuĂnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_taomoi taomoi = new Frm_taomoi();
            taomoi.ShowDialog();
            Fr_Main Main = new Fr_Main();
            Main.Hide();
        }

        private void quảnLýCôngThứcNấuĂnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_QuanLyCongThucNauAn qlct = new Frm_QuanLyCongThucNauAn();
            qlct.ShowDialog();
        }

        private void hiệnThịCôngThứcNấuĂnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_hienthi hienthi = new Frm_hienthi();
            hienthi.ShowDialog();
        }


        private string str = "***CHƯƠNG TRÌNH QUẢN LÝ CÔNG THỨC NẤU ĂN ***";
        int i = 0;
        private string str2;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (i < str.Length)
            {
                str2 += str[i];
                lblchuongtrinh.Text = str2;
                lblchuongtrinh.RightToLeft += 2;
                i++;
            }
            else
            {
                i = 0;
                str2 = "";
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.checkClick == false)
            {
                // Thông tin tìm kiếm
                if (panel2.Enabled == true)
                {
                 
                    int dong;
                    dong = e.RowIndex;
                    txtma.Text = dgvmain.Rows[dong].Cells[0].Value.ToString();
                    txtten.Text = dgvmain.Rows[dong].Cells[1].Value.ToString();
                }
            }
            else
            {
                // Thông tin phân loại
                panel2.Enabled = false;
                int dong = e.RowIndex;
                lblmapl.Text = dgvmain.Rows[dong].Cells[0].Value.ToString();
                lbltenpl.Text = dgvmain.Rows[dong].Cells[1].Value.ToString();
                lblpl.Text = dgvmain.Rows[dong].Cells[2].Value.ToString();
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void cleartimkiem()
        {
            txtma.Text = "";
            txtten.Text = "";
        }
        private void clearphanloai()
        {
            lblmapl.Text = "";
            lbltenpl.Text = "";
            lblpl.Text = "";
        }
        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.checkClick = true;
            panel3.Enabled = true;
            panel2.Enabled = false;
            cleartimkiem();
            string select = null;
            int check = 0;

            if (e.Node.Text == "Món Kho")
            {
                select = " select * from vw_congthuc where [Phân Loại]='" + e.Node.Text + "'";
                DataTable dt = new DataTable();


                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }

            if (e.Node.Text == "Món Xào")
            {
                select = " select * from vw_congthuc where [Phân Loại]='" + e.Node.Text + "'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Món Nướng")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Món Nướng'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Món Trộn ")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Món Trộn'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Món Chiên")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Món Chiên'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Món Hấp")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Món Hấp'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Món Xốt")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Món Xốt'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Món Hầm")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Món Hầm'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Súp/Cháo")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Súp/Cháo'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
            if (e.Node.Text == "Gỏi/Cuốn")
            {
                select = " select * from vw_congthuc where [Phân Loại]=N'Gỏi/Cuốn'";
                check = cnn.Execute(select);
                dgvmain.DataSource = cnn.SelectTable(select);

            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            this.checkClick = false;
            panel2.Enabled = true;
            panel3.Enabled = false;
            clearphanloai();
            {
                string tk = "select * from vw_congthucnauan where [Tên Công Thức] like N'%" + @txttimkiem.Text + "%'";
                DataTable dt = new DataTable();
                dt = cnn.SelectTable(tk);
                if (dt.Rows.Count <= 0)
                {
                    if (MessageBox.Show("Món ăn này chưa có.\nBạn có muốn thêm món ăn này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        Frm_taomoi tm = new Frm_taomoi();
                        tm.ShowDialog();
                    }
                }
                else
                {

                    dgvmain.DataSource = cnn.SelectTable(tk);
                }

            }
        }

        private void dataGriview1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {/*
            if (panel3.Enabled == true)
            {
                panel2.Enabled = false;
                int dong;

                dong = e.RowIndex;

                txtma.Text = "";
                txtten.Text = "";

                lblmapl.Text = dgvmain.Rows[dong].Cells[0].Value.ToString();
                lbltenpl.Text = dgvmain.Rows[dong].Cells[1].Value.ToString();
                lblpl.Text = dgvmain.Rows[dong].Cells[2].Value.ToString();
            }
            */
            
        }

        private void btnxemchitiet_Click(object sender, EventArgs e)
        {
            Frm_chitiet ct = new Frm_chitiet();
            Crpmain rptmain = new Crpmain();

            // DataTable dt = cnn.SelectTable("select * from vw_congthucnauan where ma='" + lblma.Text + "'");

            DataTable dt = cnn.SelectTable("select * from vw_CongThucNauAn where [Mã công thức]='" + txtma.Text + "'");
            rptmain.SetDataSource(dt);
            ct.crtviewer.ReportSource = rptmain;
            ct.ShowDialog();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void treeView2_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Frm_chitiet ct = new Frm_chitiet();
            Crpmain rptmain = new Crpmain();

            // DataTable dt = cnn.SelectTable("select * from vw_congthucnauan where ma='" + lblma.Text + "'");

            DataTable dt = cnn.SelectTable("select * from vw_CongThucNauAn where [Mã công thức]='" + lblmapl.Text + "'");
            rptmain.SetDataSource(dt);
            ct.crtviewer.ReportSource = rptmain;
            ct.ShowDialog();
        }

    }
}